<?php
/**
 * @title 2497. 图中最大星和 - 最小堆
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $vals
     * @param Integer[][] $edges
     * @param Integer $k
     * @return Integer
     */
    function maxStarSum($vals, $edges, $k) {
        if($k == 0) return max($vals);

        /**
         * @since 2024-05-17 每个节点作为中心点，与之相邻的节点集合堆
         */
        $n = count($vals);
        $heapArray = $sumArray = [];
        for($i=0; $i<$n; $i++){
            $heapArray[] = new SplMinHeap();
            $sumArray[] = $vals[$i];
        }

        /**
         * @since 2024-05-17 遍历路径
         */
        foreach ($edges as list($s, $e)){
            list($heapArray[$s], $sumArray[$s]) = $this->InsertHeap($heapArray[$s], $sumArray[$s], $vals[$e], $k);
            list($heapArray[$e], $sumArray[$e]) = $this->InsertHeap($heapArray[$e], $sumArray[$e], $vals[$s], $k);
        }

        /**
         * @since 2024-05-17 请你返回 至多 包含 k 条边的星图中的 最大星和
         * @return
         */
        return max($sumArray);
    }

    /**
     * @title 值入堆
     * @author start2004
     * @since 2024-05-17 Created
     *
     * @param SplMinHeap $minHeap 最小堆
     * @param int $sum 堆和
     * @param int $v 插入值
     * @param int $k 堆元素数量上限
     * @return [SplMinHeap, int]
     */
    function InsertHeap($minHeap, $sum, $v, $k){
        if($v > 0){
            /**
             * @since 2024-05-17 数量不足k，入堆
             */
            if($minHeap->count() < $k){
                $minHeap->insert($v);
                $sum += $v;
            } elseif($minHeap->top() < $v){
                /**
                 * @since 2024-05-17 更大值入堆
                 */
                $sum = $sum - $minHeap->top() + $v;
                $minHeap->extract();
                $minHeap->insert($v);
            } else {}
        } else {}

        /**
         * @return
         */
        return [$minHeap, $sum];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202405/2024.05.17_2.php
 */
$datas = [
    [[1,2,3,4,10,-10,-20], [[0,1],[1,2],[1,3],[3,4],[3,5],[3,6]], 2], // 16
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->maxStarSum(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
